package rulebase;

import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import lexicon.Lexicon;


public class TableSpelloutRulePanel extends JPanel
{
	private JTable table;
	private JComboBox cbxTableTriggerWord;
	private JComboBox cbxTableBaseForm;
	private JComboBox cbxTableModification;	
	private JButton btnAddRow;
	private JButton btnDeleteRow;
	private JButton btnAddColumn;
	private JButton btnDeleteColumn;
	private JButton btnTableTriggerWord;
	private JCheckBox chkTableReduplication;
	
	private ArrayList<Lexicon> lexiconList = new ArrayList<>();
	
	public TableSpelloutRulePanel()
	{
		setLayout(null);
		initGUI();
	}
	
	private void initGUI()
	{
		table = new JTable();
		
		JLabel lblTableModification = new JLabel("Modification");
		lblTableModification.setBounds(10, 79, 78, 14);
		add(lblTableModification);
		
		JLabel lblTableBaseForm = new JLabel("Base Form");
		lblTableBaseForm.setBounds(10, 45, 78, 14);
		add(lblTableBaseForm);
		
		cbxTableModification = new JComboBox();
		cbxTableModification.setBounds(98, 75, 150, 20);
		add(cbxTableModification);
		initCbxTableModificationItems();
	
		cbxTableTriggerWord = new JComboBox();
		cbxTableTriggerWord.setBounds(98, 11, 300, 20);
		add(cbxTableTriggerWord);
		
		cbxTableBaseForm = new JComboBox();
		cbxTableBaseForm.setBounds(98, 42, 150, 20);
		add(cbxTableBaseForm);
		
		chkTableReduplication = new JCheckBox("Reduplication");
		chkTableReduplication.setBounds(254, 74, 89, 23);
		add(chkTableReduplication);

		btnTableTriggerWord = new JButton("T W");
		btnTableTriggerWord.setBounds(10, 11, 57, 23);
		add(btnTableTriggerWord);
		
		btnAddRow = new JButton("Add Row");
		btnAddRow.setBounds(127, 353, 89, 23);
		add(btnAddRow);
		
		btnDeleteRow = new JButton("Del Row");
		btnDeleteRow.setBounds(127, 387, 89, 23);
		add(btnDeleteRow);
		
		btnAddColumn = new JButton("Add Column");
		btnAddColumn.setBounds(233, 353, 89, 23);
		add(btnAddColumn);
		
		btnDeleteColumn = new JButton("Del Column");
		btnDeleteColumn.setBounds(233, 387, 89, 23);
		add(btnDeleteColumn);
		
		JScrollPane spTable = new JScrollPane();
		spTable.setBounds(10, 127, 424, 215);
		add(spTable);
		spTable.setViewportView(table);
	}

	private void initCbxTableModificationItems()
	{
		DefaultComboBoxModel model = new DefaultComboBoxModel();
		model.addElement("Prefix");
		model.addElement("Suffix");
		model.addElement("Infix");
		model.addElement("Circumfix");
		model.addElement("Add Word");
		model.addElement("New Translation");
		cbxTableModification.setModel(model);
	}

	public void addTriggerWord(Lexicon lex){
		lexiconList.add(lex);
		initCbxSimpleTriggerWord();
	}
	
	public void initCbxSimpleTriggerWord(){
		DefaultComboBoxModel model = new DefaultComboBoxModel();
		
		for(int i=0; i<lexiconList.size(); i++)
			model.addElement(lexiconList.get(i).getName() + " - " + lexiconList.get(i).getGloss());
		
		cbxTableTriggerWord.setModel(model);
	}
	
	public void setCbxTableModificationListener(ActionListener AL)
	{
		cbxTableModification.addActionListener(AL);
	}
	
	public void setCbxTableBaseFormListener(ActionListener AL)
	{
		cbxTableBaseForm.addActionListener(AL);
	}
	
	public void setCbxTableTriggerWordListener(ActionListener AL)
	{
		cbxTableTriggerWord.addActionListener(AL);
	}

	public void setAddRowListener(ActionListener AL)
	{
		btnAddRow.addActionListener(AL);
	}
	
	public void setAddColumnListener(ActionListener AL)
	{
		btnAddColumn.addActionListener(AL);
	}
	
	public void setDeleteRowListener(ActionListener AL)
	{
		btnDeleteRow.addActionListener(AL);
	}
	
	public void setDeleteColumnListener(ActionListener AL)
	{
		btnDeleteColumn.addActionListener(AL);
	}
	
	public void setTableTriggerWordListener(ActionListener AL)
	{
		btnTableTriggerWord.addActionListener(AL);
	}
	
	public void setChkTableReduplicationListener(ItemListener AL)
	{
		chkTableReduplication.addItemListener(AL);
	}
}
